/*
 * @Author: your name
 * @Date: 2020-10-16 16:16:11
 * @LastEditTime: 2020-10-21 16:30:50
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /Project/node/routes/index.js
 */
const express = require('express');
// const { jwtAuth, decode } = require('../utils/user-jwt'); // 引入jwt认证函数
const router = express.Router(); // 注册路由 
// router.use(jwtAuth); // 注入认证模块

const userRouter = require('./users'); // 引入user路由模块
const foodRouter = require('./Food')
const OrderRouter = require('./Order')

// const taskRouter = require('./tasks'); // 引入task路由模块
router.use('/api', userRouter); // 注入用户路由模块
router.use('/api',foodRouter);
router.use('/api',OrderRouter);


// 自定义统一异常处理中间件，需要放在代码最后
router.use((err, req, res, next) => {
    // 自定义用户认证失败的错误返回
    // console.log('err===', err);
    if (err && err.name === 'UnauthorizedError') {
      const { status = 401, message } = err;
      // 抛出401异常
      res.status(status).json({
        code: status,
        msg: message || '不知道错误信息',
        data: null
      })
    } else {
      const { output } = err || {};
      // 错误码和错误信息
      const errCode = (output && output.statusCode) || 500;
      const errMsg = (output && output.payload && output.payload.message) || err.error;
      res.status(errCode).json({
        code: errCode,
        msg: errMsg
      })
    }
  })
  
  module.exports = router;